/* 
 * File:   main.cpp 
 * Author: Fei Fan
 * E-mail: ffinuyasha@sina.com
 * Copyright © 2013 Fei Fan, All Rights Reserved
 */

#include"FileMng.h"

char MyName[20];
char MyPassword[17];
int MemoNum;
//BaseSect baseMemo;
//MemoOpenFile openFileMemo[OPEN_MAX];
//int openedSize;
//int openedSum;
//char strMemo[STRMEMO_MAX][CONTENT_MAX];

int main() {
    char cmd[10], name[20], newName[20], path[50],tempChar[50];
    char opt;
    char filename[50], loadTypeStr[5];
    short loadType;
    int res;
    long modTime;
    Zone myZone;

    //	 HDformat();
    
//    for(MemoNum=0;MemoNum<USER_MAX;MemoNum++)
//        releaseMemoZone();
//    return 0;

    iniMemo();
    
    if (isExist("\0",SECT_PSDFILE)==FAIL) {
        printf("\n*******************************\n");
        printf("            REGISTRATION");
        printf("\n*******************************\n");
        registration();
        printf("Initializing....\n");
        HDformat();
        printf("SUCCESS! \n\nPlease log in again!\n\n");
    }

    //用户登录
    login(name);

    //	res=FAIL;
    //	while(res==FAIL){
    //		res=addOnline(name);
    //	}
    printf("\n*******************************\n");
    printf("    User %s is online NOW", name);
    printf("\n*******************************\n");

    //输入命令，并执行
    while (1) {
        printf("\n\n");
        //                if(strcmp(name,"root")==0)
        //                    printf(" [format] Format harddisk   [adduser] Add a user  [deluser] Delete a user\n");
        printf(" [load] Load a file to the memory\t [unload] Unload a file from memory\n");
        printf(" [show] Show the content of a file\t [edit] Edit the content of a file\n");
        printf(" [create] Create a new file\t\t [delete] Delete a file\n");
        printf(" [format] Format harddisk\t [quit] Quit\n");
        gets(cmd);
        if (strcmp(cmd, "create") == 0) {//创建文件
            printf("Please input the directory that the file will be created:");
            gets(path);
            //是否在自己的路径下
            if(isRemote(path,tempChar,tempChar)==SUCCESS){
                printf("Sorry! NO PERMISSION to create a file in directories of other users!\n");
                showOpen();
                continue;
            }
            if (isSys(path)) {
                printf("Sorry! NO PERMISSION to create a file in /sys directories!\n");
                showOpen();
                continue;
            }
            printf("Please input the file name:");
            gets(filename);

            res=createFile(path, filename);
            if (res != FAIL)
                printf("Create SUCCESSFULY!\n");
            else
                printf("Create FAILED!\n");
        } else if (strcmp(cmd, "load") == 0) { //打开文件
            printf("Please input the file name that you want to load:");
            gets(filename);
            printf("Please choose the type of loading\n(r: READ ONLY w:WRITE ONLY rw:READ AND WRITE):");
            while (1) {
                gets(loadTypeStr);
                if (strcmp(loadTypeStr, "rw") == 0) {
                    loadType = RANDW;
                    break;
                } else if (strcmp(loadTypeStr, "r") == 0) {
                    loadType = READ;
                    break;
                } else if (strcmp(loadTypeStr, "w") == 0) {
                    loadType = WRITE;
                    break;
                } else {
                    printf("The value you entered is NOT valid, please input again!\n");
                }
            }

            res = loadFile(filename, loadType);
            if (res != FAIL)
                printf( "Load SUCCESSFULY!\n");
            else
                printf("Load FIALED!\n");
        } else if (strcmp(cmd, "delete") == 0) { //删除文件
            printf("Please input the file name that you want to delete:");
            gets(filename);
            //是否在自己的路径下
            if(isRemote(filename,tempChar,tempChar)==SUCCESS){
                printf("Sorry! NO PERMISSION to delete a file in directories of other users!\n");
                showOpen();
                continue;
            }
            if (isSys(filename)) {
                printf("Sorry! NO PERMISSION to delete a file in /sys directories!\n");
                showOpen();
                continue;
            }
            if(strcmp(filename,"/")==0){
                printf("Sorry! NO PERMISSION to delete a file in / directories!\n");
                showOpen();
                continue;
            }
            res = deleteFile(filename);
            if (res != FAIL)
                printf("Delete SUCCESSFULY!\n");
            else
                printf("Delete FAILED!\n");
        } else if (strcmp(cmd, "unload") == 0) { //关闭文件
            printf("Please input the file name that you want to unload:");
            gets(filename);
            res = unloadFile(filename);
            if (res != FAIL)
                printf("Unload SUCCESSFULY!\n");
            else
                printf("Unload FAILED!\n");
        } else if (strcmp(cmd, "show") == 0) { //显示文件
            printf("Please input the file name that you want to show:(the type of loading is READ ONLY or READ AND WRITE):");
            gets(filename);
            res = showFile(filename);
            if (res == FAIL)
                printf("Show FAILED!\n");
        } else if (strcmp(cmd, "edit") == 0) { //写文件
            printf("Please input the file name that you want to edit:(the type of loading is WRITE ONLY or READ AND WRITE):");
            gets(filename);
            res = editFile(filename);
            if (res != FAIL)
                printf("Edit SUCCESSFULY!\n");
            else
                printf("Edit FAILED!\n");
        } else if (strcmp(cmd, "quit") == 0) {
            //检查是否关闭所有文件
            getMemoZone(&myZone);
            if (myZone.openedSum != 0)
                printf("Please UNLOAD all of file before quit!\n");
            else{
                releaseMemoZone();
                break;
            }
        } else if (strcmp(cmd, "format") == 0) {
            //                        if(strcmp(name,"root")){
            //                            printf("The value you entered is NOT valid!\n");
            //                            continue;
            //                        }
            getMemoZone(&myZone);
            if (myZone.openedSum != 0){
                printf("Please UNLOAD all of file before format!\n");
                showOpen();
                continue;
            }
            printf("\n**********************************\n");
            printf("    CONFIRM:[Y]Format [N]Cancel");
            printf("\n**********************************\n");
            while (1) {
                opt=getche();
                printf("\n");
                if (opt == 'y' || opt == 'Y') {
                    HDformat();
                    //addOnline(name);
                    break;
                } else if (opt == 'n' || opt == 'N')
                    break;
                else
                    printf("The value you entered is NOT valid, please input again:");
            }
            printf("Format SUCCESSFULLY!\n");

        }//                else if(strcmp(cmd,"adduser")==0){
            //                        if(strcmp(name,"root")){
            //                            printf("The value you entered is NOT valid!\n");
            //                            continue;
            //                        }
            //
            //			time(&modTime);
            //			res=addUser(newName,modTime);
            //			if(res!=FAIL){
            //				addUserHead(newName,modTime);
            //				printf("User %s is SUCCESSFULY added!\n",newName);
            //			}
            //
            //                }
            //                else if(strcmp(cmd,"deluser")==0){
            //                        if(strcmp(name,"root")){
            //                            printf("The value you entered is NOT valid!\n");
            //                            continue;
            //                        }
            //                        
            //                        res=onlyRoot();
            //			if(res==FAIL)
            //				continue;
            //			res=delUser(newName);
            //			if(res!=FAIL){
            //				delUserHead(newName);
            //				printf("User %s is SUCCESSFULY deleted!\n",newName);
            //			}
            //                }
        else
            printf("The value you entered is NOT valid!\n");

        showOpen();
    }
    //	res=FAIL;
    //	while(res==FAIL){
    //		res=delOnline(name);
    //	}

    return 0;
}

